package com.globallogic.dailyplanner.configuration;

import java.util.Properties;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.hibernate.ejb.HibernatePersistence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages={"com.globallogic.dailyplanner.repository"})
@PropertySource("classpath:appTest.properties")
@ComponentScan("com.globallogic.dailyplanner.service")
public class DataBaseTestConfig {
	private static final String PROP_DATABASE_DRIVER = "test.db.driver";
    private static final String PROP_DATABASE_PASSWORD = "test.db.password";
    private static final String PROP_DATABASE_URL = "test.db.url";
    private static final String PROP_DATABASE_USERNAME = "test.db.username";
 
    private static final String PROP_HIBERNATE_DIALECT = "test.hibernate.dialect";
    private static final String PROP_HIBERNATE_SHOW_SQL = "test.hibernate.show_sql";
    private static final String PROP_TEST_HIBERNATE_HBM2DDL_AUTO = "test.hibernate.hbm2ddl.auto";
    private static final String PROP_HIBERNATE_HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";
    private static final String PROP_ENTITYMANAGER_PACKAGES_TO_SCAN = "test.entitymanager.packages.to.scan";
    
    @Resource
    private Environment env;
    
    @Bean
    public DataSource dataSource(){
    	DriverManagerDataSource dataSource = new DriverManagerDataSource();
    	
    	dataSource.setDriverClassName(env.getProperty(PROP_DATABASE_DRIVER));
    	dataSource.setUrl(env.getProperty(PROP_DATABASE_URL));
    	dataSource.setUsername(env.getProperty(PROP_DATABASE_USERNAME));
    	dataSource.setPassword(env.getProperty(PROP_DATABASE_PASSWORD));
    	
    	return dataSource;
    }
    
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
    	LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    	entityManagerFactoryBean.setDataSource(dataSource());
    	entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);
    	entityManagerFactoryBean.setPackagesToScan(env.getProperty(PROP_ENTITYMANAGER_PACKAGES_TO_SCAN));
    	
    	entityManagerFactoryBean.setJpaProperties(getHibernateProperties());
    	
    	return entityManagerFactoryBean;
    }
    
    @Bean
    public JpaTransactionManager transactionManager(){
    	JpaTransactionManager manager = new JpaTransactionManager();
    	manager.setEntityManagerFactory(entityManagerFactory().getObject());
    	
    	return manager;
    }
    
    private Properties getHibernateProperties() {
        Properties properties = new Properties();
        properties.put(PROP_HIBERNATE_DIALECT, env.getRequiredProperty(PROP_HIBERNATE_DIALECT));
        properties.put(PROP_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROP_HIBERNATE_SHOW_SQL));
        properties.put(PROP_HIBERNATE_HBM2DDL_AUTO, env.getRequiredProperty(PROP_TEST_HIBERNATE_HBM2DDL_AUTO));
 
        return properties;
    }
	
}
